package com.captain.special.数组;

import java.util.*;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/4/17 22:14
 */
public class 找到和最大的长度为K的子序列2099 {
    public static void main(String[] args) {
        new 找到和最大的长度为K的子序列2099().maxSubsequence(new int[]{-1,-2,3,4}, 3);
    }

    public int[] maxSubsequence(int[] nums, int k) {
        int[] tp = Arrays.copyOf(nums, nums.length);
        Arrays.sort(tp);
        //取最大的值;
        int originalIndex = nums.length - 1;
        int index = originalIndex - k;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = nums.length - 1; i > index; i--) {
            map.put(tp[i], map.getOrDefault(tp[i], 0) + 1);
        }
        int[] arr = new int[k];
        int aIndex = 0;
        for (int num : nums) {
            Integer defaultValue = map.getOrDefault(num, 0);
            if (defaultValue == 0) {
                continue;
            }
            arr[aIndex++] = num;
            map.put(num, map.get(num) - 1);
            //已经找完了
            if (aIndex >= k) {
                break;
            }
        }
        return arr;
    }
}
